package com.example.home.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.home.activity.R;
import com.example.home.adapter.ChannelAdapter;
import com.example.home.adapter.HomeBrandAdapter;
import com.example.home.adapter.HomeCouponAdapter;
import com.example.home.adapter.HomeGrouponAdapter;
import com.example.home.adapter.HomeTopicAdapter;
import com.example.home.adapter.HotGoodsAdapter;
import com.example.home.adapter.NewGoodsAdapter;
import com.example.home.api.ApiService;
import com.example.home.interfaces.ResponseCallback;
import com.example.home.viewmodel.ResponseData;
import com.example.home.viewmodel.home.BrandList;
import com.example.home.viewmodel.home.Channel;
import com.example.home.viewmodel.home.CouponList;
import com.example.home.viewmodel.home.GrouponList;
import com.example.home.viewmodel.home.HomeData;
import com.example.home.viewmodel.home.HotGoodsList;
import com.example.home.viewmodel.home.NewGoodsList;
import com.example.home.viewmodel.home.TopicList;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    private com.youth.banner.Banner viewBanner;
    private List<com.example.home.viewmodel.home.Banner> bannerData;

    private GridView viewChannel;

    private List<Channel> channel;

    private GridView viewCoupon;

    private List<CouponList> couponLists;

    private GridView viewBrand;
    private List<BrandList> brandLists;

    private GridView viewNewGoods;
    private List<NewGoodsList> newGoodsLists;

    private GridView viewHotGoods;
    private List<HotGoodsList> hotGoodsLists;

    private GridView viewTopic;
    private List<TopicList> topicLists;
    private GridView viewGroupon;
    private List<GrouponList> grouponLists;



    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public HomeFragment() {
        // Required empty public constructor
    }

    private void initView(View view) {
        viewBanner = view.findViewById(R.id.home_banner);
        viewChannel = view.findViewById(R.id.home_channel);
        viewCoupon = view.findViewById(R.id.home_coupon);
        viewBrand = view.findViewById(R.id.home_brand);
        viewNewGoods = view.findViewById(R.id.home_newGoods);
        viewHotGoods = view.findViewById(R.id.home_hotGoods);
        viewTopic = view.findViewById(R.id.home_topicList);
        viewGroupon = view.findViewById(R.id.home_grouponList);
    }

    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);


        ApiService.GetHome(new ResponseCallback<ResponseData<HomeData>>() {
            @Override
            public void onSuccess(ResponseData<HomeData> result) {
                bannerData = result.getData().getBanner();
                channel = result.getData().getChannel();
                couponLists = result.getData().getCouponList();
                brandLists = result.getData().getBrandList();
                newGoodsLists = result.getData().getNewGoodsList();
                hotGoodsLists = result.getData().getHotGoodsList();
                topicLists = result.getData().getTopicList();
                grouponLists = result.getData().getGrouponList();
                viewBanner.setAdapter(new BannerImageAdapter<com.example.home.viewmodel.home.Banner>(bannerData) {

                            @Override
                            public void onBindView(BannerImageHolder holder, com.example.home.viewmodel.home.Banner data, int position, int size) {
                                //图片加载自己实现
                                Glide.with(holder.itemView)
                                        .load(data.getUrl())
                                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                        .into(holder.imageView);
                            }
                        })
                        .addBannerLifecycleObserver(HomeFragment.this)//添加生命周期观察者
                        .setIndicator(new CircleIndicator(view.getContext()))
                        .setIndicatorSelectedColor(Color.parseColor("#0094ff"))
                        .setIndicatorNormalColor(Color.RED);
                viewChannel.setAdapter(new ChannelAdapter(view.getContext(), channel));
                viewCoupon.setAdapter(new HomeCouponAdapter(view.getContext(),couponLists));
                viewBrand.setAdapter(new HomeBrandAdapter(view.getContext(),brandLists));
                viewNewGoods.setAdapter(new NewGoodsAdapter(view.getContext(),newGoodsLists));
                viewHotGoods.setAdapter(new HotGoodsAdapter(view.getContext(),hotGoodsLists));
                viewTopic.setAdapter(new HomeTopicAdapter(view.getContext(),topicLists));
                viewGroupon.setAdapter(new HomeGrouponAdapter(view.getContext(),grouponLists));
                viewChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    }
                });
            }
        });

        return view;
    }
}